package com.ard.camera.demos.entry;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class HostEntry {
    private String address;  // 主机地址（IP或域名）
    private String name;     // 友好名称

    public HostEntry(String address) {
        this.address = address;
        this.name = address; // 默认名称使用地址
    }

    /**
     * 从字符串解析主机条目
     * 格式：地址:名称 或 地址
     */
    public static HostEntry fromString(String line) {
        line = line.trim();
        if (line.contains(":")) {
            String[] parts = line.split(":", 2);
            String address = parts[0].trim();
            String name = parts[1].trim();
            return new HostEntry(address, name);
        } else {
            return new HostEntry(line, line);
        }
    }

    @Override
    public String toString() {
        if (address.equals(name)) {
            return address;
        }
        return address + ":" + name;
    }
}